﻿using System.Collections.Generic;
using System.Linq;
using BusinessLogic.BusinessEntity.Entities;
using BusinessLogic.Connectivity.Entities;
using System;

namespace BankApplication.BusinessLogic
{
	public class AccountsManager
	{
		public static IList<Account> GetAllAcounts(string sessionTicket)
		{
			var serverResponse = ServerProxy.Instance.GetAllAccounts(sessionTicket);

			if (serverResponse.ServerResponseCode != ServerResponseCode.Success)
			{
				throw new ServerException(serverResponse.Details);
			}

			return serverResponse.Payload;
		}

		public static void CreateAccount(Account account, string sessionTicket)
		{
			var serverResponse = ServerProxy.Instance.CreateAccount(account, sessionTicket);

			if (serverResponse.ServerResponseCode != ServerResponseCode.Success)
			{
				throw new ServerException(serverResponse.Details);
			}
		}

		public static void DeleteAccount(int accountID, string sessionTicket)
		{
			var serverResponse = ServerProxy.Instance.DeleteAccountById(accountID, sessionTicket);

			if (serverResponse.ServerResponseCode != ServerResponseCode.Success)
			{
				throw new ServerException(serverResponse.Details);
			}
		}

		public static void ReplenishAccount(int accountID, decimal sumToAdd, string sessionTicket)
		{
			var serverResponse = ServerProxy.Instance.ReplenishAccount(accountID, sumToAdd, sessionTicket);

			if (serverResponse.ServerResponseCode != ServerResponseCode.Success)
			{
				throw new ServerException(serverResponse.Details);
			}
		}


		public static IList<DocumentEnvelope> GetFilteredEnvelopes(string userName, DateTime periodStart, DateTime periodEnd, string sessionTicket)
		{
			var serverResponse = ServerProxy.Instance.GetFilteredEnvelopes(userName, periodStart, periodEnd, sessionTicket);

			if (serverResponse.ServerResponseCode != ServerResponseCode.Success)
			{
				throw new ServerException(serverResponse.Details);
			}

			return serverResponse.Payload;
		}
	}
}
